package org.example;

import groovy.lang.GroovyShell;
import org.apache.shardingsphere.elasticjob.infra.env.HostException;
import org.apache.shardingsphere.elasticjob.infra.env.IpUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * @author yangzhenyu
 * @version 1.0
 * @description:
 * @date 2023/4/26 9:46
 */
@EnableAsync
@SpringBootApplication(scanBasePackages={"org.example"})
public class DemoElasticJob {
    public static void main(String[] args) {
        shieldElasticJobIpIsNull();
        SpringApplication.run(DemoElasticJob.class, args);
    }

    /**
     * 屏蔽org.apache.shardingsphere.elasticjob.infra.env.IpUtils.getIp()抛出
     * HostException(ip is null) 的异常导致windows本地程序无法启动
     */
    private static void shieldElasticJobIpIsNull() {
        try {
            IpUtils.getIp();
        } catch (HostException e) {
            //抛出HostException 且 异常信息为 "ip is null" 时，设置ip地址为 0.0.0.0
            if ("ip is null".equals(e.getMessage())) {
                String code = "org.apache.shardingsphere.elasticjob.infra.env.IpUtils.cachedIpAddress=\"0.0.0.0\";";
                GroovyShell groovy = new GroovyShell();
                groovy.evaluate(code);
            }
        }
    }

}
